﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace G.Model
{
    public class GameValueDef : IEquatable<GameValueDef>
    {
        private GameSpaceDef _GameDef;
        private string _Name;
        private string _Value;
        public bool _Display;
        public System.TypeCode _TypeCode;
        
        public GameValueDef() 
        {
            _Name = "Gold";
            _TypeCode = TypeCode.Int32;
            _Display = true;
        }

        public GameValueDef(string pName, TypeCode pCode = TypeCode.String) 
        {
            _Name = pName;
            _TypeCode = pCode;
            if (pCode == TypeCode.Boolean)
            {
                _Value = "false";
            }
        }

        public GameValueDef Copy()
        {
            return (GameValueDef)base.MemberwiseClone();
        }

        [Browsable(false), IgnoreDataMember]
        public GameSpaceDef GameDef
        {
            get { return _GameDef; }
            set { _GameDef = value; }
        }
        
        public string Name
        {
            get { return _Name; }
            set 
            {
                if (_Name != value)
                {
                    _Name = value;
                    SetDirty();                
                }
            }
        }

        public string Value
        {
            get { return _Value; }
            set 
            {
                if (_Value != value)
                {
                    _Value = value;
                    SetDirty();                
                }
            }
        }
        
        public bool Display
        {
            get { return _Display; }
            set 
            {
                if (_Display != value)
                {
                    _Display = value;
                    SetDirty();                
                }
            }
        }
        
        public System.TypeCode TypeCode
        {
            get { return _TypeCode; }
            set 
            {
                if (_TypeCode != value)
                {
                    _TypeCode = value;
                    SetDirty();
                }
            }
        }

        public bool Equals(GameValueDef other)
        {
            return (this.Name == other.Name);
        }

        public static bool operator ==(GameValueDef a, GameValueDef b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object)a == null) || ((object)b == null))
            {
                return false;
            }
            return a.Equals(b);
        }

        public static bool operator !=(GameValueDef a, GameValueDef b)
        {
            return !(a == b);
        }

        public override bool Equals(object other)
        {
            if ((object)other == null)
            {
                return false;
            }

            return (this.Name == ((GameValueDef)other).Name);
        }

        public override int GetHashCode()
        {
            return this.Name.GetHashCode();
        }

        public override string ToString()
        {
            return Name;
        }
       
        private void SetDirty()
        {
            if (_GameDef != null)
            {
        	   _GameDef.IsDirty = true;
            }
        }
    }

}
